package fr.androguies.powerball.scene;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.MathUtils;

import android.util.Log;

public class LauncherScene extends Scene {
	
	private BaseGameActivity context;

	public LauncherScene(BaseGameActivity context) {
		this.context = context;
		setBackground(new ColorBackground(10, 10, 10));
		setTouchAreaBindingEnabled(true);
	}
	
	public void loadResources() {
		TextureRegionFactory.setAssetBasePath("gfx/");
		
		
		Texture launcherCanonTexture = new Texture(1024, 1024);
		TextureRegion launcherCanonTextureRegion = TextureRegionFactory.createFromAsset(launcherCanonTexture, context, "canon.png", 0, 0);
		
		context.getEngine().getTextureManager().loadTextures(launcherCanonTexture);
		
		Sprite launcherCanonSprite = new Sprite(20, context.getEngine().getCamera().getHeight() - 48, launcherCanonTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				this.setRotation(-MathUtils.radToDeg((float)Math.atan2(-pTouchAreaLocalY, pTouchAreaLocalX)));
				Log.d("Sprite Touch", "x: " + pTouchAreaLocalX + " , y: " + pTouchAreaLocalY + ", deg: " + -MathUtils.radToDeg((float)Math.atan2(-pTouchAreaLocalY, pTouchAreaLocalX)));
				return true;
			}

		};
		registerTouchArea(launcherCanonSprite);
		launcherCanonSprite.setRotationCenter(5, launcherCanonSprite.getHeight() / 2);
		launcherCanonSprite.setHeight(25);
		launcherCanonSprite.setWidth(60);
		
		attachChild(launcherCanonSprite);
		
		
		Texture launcherBaseTexture = new Texture(1024, 1024);
		TextureRegion launcherBaseTextureRegion = TextureRegionFactory.createFromAsset(launcherBaseTexture, context, "socle.png", 0, 0);

		context.getEngine().getTextureManager().loadTextures(launcherBaseTexture);
		
		Sprite launcherBaseSprite = new Sprite(10, context.getEngine().getCamera().getHeight() - 40, launcherBaseTextureRegion);
		launcherBaseSprite.setHeight(30);
		launcherBaseSprite.setWidth(50);
		
		attachChild(launcherBaseSprite);
	}

}
